package com.example.project3.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.project3.R;
import com.example.project3.model.Book;
import java.util.List;
import java.util.ArrayList;

/**
 * 书籍列表适配器
 * 用于在RecyclerView中展示书籍列表
 */
public class BookAdapter extends RecyclerView.Adapter<BookAdapter.BookViewHolder> {
    
    private List<Book> bookList = new ArrayList<>();
    private OnBookActionListener onBookActionListener;

    /**
     * 书籍操作接口
     */
    public interface OnBookActionListener {
        void onEditBook(Book book, int position);
        void onDeleteBook(Book book, int position);
    }

    public void setOnBookActionListener(OnBookActionListener listener) {
        this.onBookActionListener = listener;
    }

    /**
     * 更新书籍列表
     */
    public void updateBookList(List<Book> newBookList) {
        this.bookList.clear();
        if (newBookList != null) {
            this.bookList.addAll(newBookList);
        }
        notifyDataSetChanged();
    }

    /**
     * 添加书籍
     */
    public void addBook(Book book) {
        bookList.add(0, book); // 添加到列表顶部
        notifyItemInserted(0);
    }

    /**
     * 更新指定位置的书籍
     */
    public void updateBook(int position, Book book) {
        if (position >= 0 && position < bookList.size()) {
            bookList.set(position, book);
            notifyItemChanged(position);
        }
    }

    /**
     * 删除指定位置的书籍
     */
    public void removeBook(int position) {
        if (position >= 0 && position < bookList.size()) {
            bookList.remove(position);
            notifyItemRemoved(position);
        }
    }

    @NonNull
    @Override
    public BookViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_book, parent, false);
        return new BookViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull BookViewHolder holder, int position) {
        Book book = bookList.get(position);
        holder.bind(book, position);
    }

    @Override
    public int getItemCount() {
        return bookList.size();
    }

    /**
     * ViewHolder类
     */
    class BookViewHolder extends RecyclerView.ViewHolder {
        private TextView tvBookName;
        private TextView tvBookNumber;
        private TextView tvAuthor;
        private TextView tvPrice;
        private Button btnEdit;
        private Button btnDelete;

        public BookViewHolder(@NonNull View itemView) {
            super(itemView);
            tvBookName = itemView.findViewById(R.id.tv_book_name);
            tvBookNumber = itemView.findViewById(R.id.tv_book_number);
            tvAuthor = itemView.findViewById(R.id.tv_author);
            tvPrice = itemView.findViewById(R.id.tv_price);
            btnEdit = itemView.findViewById(R.id.btn_edit);
            btnDelete = itemView.findViewById(R.id.btn_delete);
        }

        public void bind(Book book, int position) {
            // 设置书籍信息
            tvBookName.setText(book.getBookName());
            tvBookNumber.setText("书号：" + book.getBookNumber());
            tvAuthor.setText("作者：" + (book.getAuthor() != null ? book.getAuthor() : "未知"));
            tvPrice.setText(String.format("价格：￥%.2f", book.getPrice()));

            // 设置编辑按钮点击事件
            btnEdit.setOnClickListener(v -> {
                if (onBookActionListener != null) {
                    onBookActionListener.onEditBook(book, position);
                }
            });

            // 设置删除按钮点击事件
            btnDelete.setOnClickListener(v -> {
                if (onBookActionListener != null) {
                    onBookActionListener.onDeleteBook(book, position);
                }
            });
        }
    }
}